package com.clubmg.service.impl;

import com.clubmg.dao.ChiefDao;
import com.clubmg.dao.MemberDao;
import com.clubmg.pojo.Club;
import com.clubmg.pojo.Member;
import com.clubmg.pojo.Notice;
import com.clubmg.service.ChiefService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("chiefService")
public class ChiefServiceImpl implements ChiefService {


    @Resource(name = "chiefDao")
    private ChiefDao chiefDao;

    @Resource(name = "memberDao")
    private MemberDao memberDao;

    @Override
    public Club getClubIManaged(Long adminId) {
        return chiefDao.getClubIManaged(adminId);
    }

    @Override
    public void allowJoin(Long applyUid, Long currentUid) {

        Club myClub = chiefDao.getClubIManaged(currentUid);

        Member member = new Member();
        member.setUid(applyUid);
        member.setCid(myClub.getCid());
        member.setPermit(0);
        member.setState1(0);

        chiefDao.updateMemberApply(member);

    }

    @Override
    public void refuseQuit(Long applyUid, Long currentUid) {
        Club myClub = chiefDao.getClubIManaged(currentUid);

        Member member = new Member();
        member.setUid(applyUid);
        member.setCid(myClub.getCid());

        chiefDao.delMemberApply(member);

    }

    //注销社团
    //查询当前用户管理的社团，若结果与当前请求注销的社团相同，将其state置为-1
    @Override
    public int deregisterClub(Long currentUid, Long cid) {
        Club clubIManaged = chiefDao.getClubIManaged(currentUid);
        if (clubIManaged==null||clubIManaged.getCid()!=cid)return -1;
        clubIManaged.setState(-1);
        chiefDao.updateClubModify(clubIManaged);
        return 0;
    }

    @Override
    public void test() {
        chiefDao.test();
    }

    @Override
    public void employ(Long uid, Long cid, String position) {
        Member member = new Member();
        member.setUid(uid);
        member.setCid(cid);
        member.setPosition(position);
        member.setState2(0);
        memberDao.updateApply(member);
    }

    @Override
    public void dismissOrDeny(Long uid, Long cid, String position) {
        Member member = new Member();
        member.setUid(uid);
        member.setCid(cid);
        memberDao.updateApply(member);
    }



    @Override
    public void releaseNotice(Long cid, String title, String text) {
        Notice notice = new Notice();
        notice.setCid(cid);
        notice.setTitle(title);
        notice.setText(text);
        memberDao.saveNewNotice(notice);
    }

    @Override
    public void recallNotice(Long nid) {
        Notice notice = new Notice();
        notice.setNid(nid);
        memberDao.recallNotice(notice);
    }
}
